﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using System.Web.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class SoKmDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_SoKm> GetAllSoKm()
        {
            return DBDanhMuc.dm_SoKms.Where(p => p.IsDelete == false).ToList();
        }
        public SelectList cboSoKm(int? selected)
        {
            IList<dm_SoKm> list = this.GetAllSoKm();
            IList<object> selectlist = new List<object>{new {Value=0,Text="------ Chọn số km ------"}};

            foreach (var item in list)
            {
                selectlist.Add(new { Value = item.Id, Text = item.Name });
            }
            return new SelectList(selectlist, selected);
        }
        public dm_SoKm AddNewSoKm(dm_SoKm SoKm)
        {
            DBDanhMuc.dm_SoKms.InsertOnSubmit(SoKm);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteSoKm(int SoKmID)
        {
            dm_SoKm SoKmNew =
                DBDanhMuc.dm_SoKms.Single(p => p.Id == SoKmID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_SoKms.DeleteOnSubmit(SoKmNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteSoKm(dm_SoKm SoKm)
        {
            try
            {
                dm_SoKm SoKmDelete =
                DBDanhMuc.dm_SoKms.Single(p => p.Id == SoKm.Id);
                SoKmDelete.Id = SoKm.Id;
                SoKmDelete.UpdateDate = DateTime.Now;
                SoKmDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                SoKmDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        public void UpdateSoKm(dm_SoKm SoKm)
        {
            dm_SoKm SoKmUpdate =
                DBDanhMuc.dm_SoKms.Single(p => p.Id == SoKm.Id);

            SoKmUpdate.Id = SoKm.Id;
            SoKmUpdate.Name = SoKm.Name;
            SoKmUpdate.MoTa = SoKm.MoTa;
            SoKmUpdate.UpdateDate = SoKmUpdate.UpdateDate;
            SoKmUpdate.UpdateBy = SoKmUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_SoKm> SearchSoKmPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_SoKm>)DBDanhMuc.dm_SoKms.Where(p => p.Name.Contains(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_SoKm GetSoKmById(string id)
        {
            try
            {
                dm_SoKm item = this.DBDanhMuc.dm_SoKms.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_SoKm GetSoKmByTenCheck(string ten)
        {

            IEnumerable<dm_SoKm> list = this.DBDanhMuc.dm_SoKms.Where(p => p.Name.Equals(ten) && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_SoKm GetSoKmByTen(string ten)
        {

            IEnumerable<dm_SoKm> list = this.DBDanhMuc.dm_SoKms.Where(p => p.Name.Equals(ten));
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_SoKm> RecycleGetSoKmPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_SoKms.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_SoKm> GetSoKmPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_SoKms.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_SoKm BuildNewSoKm(SoKmForm form)
        {
            dm_SoKm newSoKm = new dm_SoKm();
            newSoKm.Name = form.Name;
            newSoKm.MoTa = form.MoTa;
            newSoKm.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newSoKm.CreateDate = DateTime.Now;
            newSoKm.IsDelete = false;
            return newSoKm;
        }
        public int PhucHoiSoKm(int id)
        {
            dm_SoKm phuchoi = new dm_SoKm();
            phuchoi = this.GetSoKmById(Convert.ToString(id));
            dm_SoKm item = new dm_SoKm();
            item = this.GetSoKmByTenCheck(phuchoi.Name);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_SoKm BuildEditSoKm(SoKmForm form)
        {
            dm_SoKm newSoKm = new dm_SoKm();
            newSoKm.Id = form.Id;
            newSoKm.Name = form.Name;
            newSoKm.MoTa = form.MoTa;
            newSoKm.UpdateDate = DateTime.Now;
            newSoKm.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newSoKm;
        }
        public dm_SoKm GetLoaiCongChucIdMax()
        {
            IList<dm_SoKm> list = DBDanhMuc.dm_SoKms.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public SoKmForm ConvertToForm(dm_SoKm obj)
        {
            SoKmForm form = new SoKmForm();
            form.Id = obj.Id;
            form.Name = obj.Name;
            form.MoTa = obj.MoTa;
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}